package com.admin.service;

import com.admin.dbdao.SoftwareExamineDao;
import com.data.util.CheckDataUtils;
import com.data.util.DatabaseIdUtils;
import com.data.util.FileResumeUtils;
import com.user.bean.AppIcon;
import com.user.bean.AppRefuse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 软件信息管理
 *
 * @author:HingLo
 * @create 2017-09-21 10:21
 **/
@Service
public class SoftwareExamineService {
    @Autowired
    private SoftwareExamineDao softwareExamineDao;


    public List<?> findSoftwareService(int type, String fid) {
        return softwareExamineDao.findSoftwareDao(type, fid);
    }

    /**
     * 请求图片
     *
     * @param id   软件的id
     * @param type 图片的类型
     * @return
     */
    public byte[] lookImageService(String id, String type) {
        id = id.trim();
        if (CheckDataUtils.stringUtils(id, type)) {
            List<AppIcon> list = softwareExamineDao.findSoftwareIconService(id, type);
            if (list.size() > 0) {
                String name = list.get(0).getName();
                return FileResumeUtils.readMinIcon(id, name);
            }
        }
        //读取默认图片
        return FileResumeUtils.readMinIcon("default", "default.png");
    }


    public List<AppIcon> findSoftwareIconService(String id, String type) {
        if (!CheckDataUtils.stringUtils(id, type)) {
            return null;
        }
        id = id.trim();
        type = type.trim();
        List<AppIcon> list = softwareExamineDao.findSoftwareIconService(id, type);
        return list.size() > 0 ? list : null;

    }

    /**
     * 根据软件的id  返回软件的md5值
     *
     * @param rid
     * @return
     */
    public String findApkService(String rid) {
        rid = rid.trim();
        return softwareExamineDao.findApkDao(rid);
    }

    /**
     * 通过审核
     *
     * @param id
     * @return
     */
    public boolean softwarePassService(String id, int state) {
        id = id.trim();
        return softwareExamineDao.updateSoftwareDao(id, state);
    }

    /**
     * 不通过该软件的审核
     *
     * @param id
     * @param state
     * @param contents
     * @return
     */
    public boolean softwareRefuseService(String id, int state, String contents) {
        id = id.trim();
        contents = contents.trim();
        AppRefuse appRefuse = new AppRefuse();
        appRefuse.setCtime(new Date());
        appRefuse.setRid(id);
        appRefuse.setId(DatabaseIdUtils.getDataIdOne());
        appRefuse.setContent(contents);
        return softwareExamineDao.updateSoftwareRefuseDao(state, appRefuse);


    }
}
